package com.betterwood.dragon.service;

import com.betterwood.dragon.dao.SysUserDao;
import com.betterwood.dragon.entity.SysUser;
import com.betterwood.dragon.entity.User;

import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import cn.hutool.core.util.ObjectUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class SysUserService {

    // 注入新的CURD操作类
    @Autowired
    private SysUserDao sysUserDao;

    public SysUser insert(SysUser record) {
        return sysUserDao.insert(record);
    }

    public void update(SysUser record) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        sysUserDao.updateSysUsertest(record);
    }

    public Optional<SysUser> queryById(String record) {
        return sysUserDao.findById(record);
    }

    public void deleteById(String id) {
        sysUserDao.deleteById(id);
    }

    public SysUser getSysUserByNameAndPwd(String account, String password) {
        SysUser sys = sysUserDao.getByUserNameAndPassword(account, password);
        return sys;
    }

    public List<SysUser> findAll() {
        return sysUserDao.findAll();
    }

    public boolean isExist(String userName) {
        List<SysUser> sysUsers = sysUserDao.findByUserName(userName);
        if (ObjectUtil.isNotEmpty(sysUsers)) {
            return true;
        }
        return false;
    }

    public Optional<SysUser> findOneById(String id) {
        Optional<SysUser> sysUser = sysUserDao.findById(id);
        // if sysUser.ifPresent(u -> System.out.println());
        return sysUser;
    }

    public SysUser findByOpenId(String openId) {
        return sysUserDao.getByOpenId(openId);
    }
}
